package com.ybwh.springboot2.sharding5.ext.kengen;

import org.apache.shardingsphere.infra.instance.InstanceContext;
import org.apache.shardingsphere.infra.instance.InstanceContextAware;
import org.apache.shardingsphere.sharding.spi.KeyGenerateAlgorithm;

import java.util.Properties;

/**
 * <p>
 * 对接美团Leafid生成器,需要在org.apache.shardingsphere.spi.keygen.ShardingKeyGenerator文件中配置类名才可使用
 * </p>
 *
 * @author fanbeibei
 * @version 1.0.0
 * @date 2023/4/12 14:46
 */
public class LeafidKeyGenerator implements KeyGenerateAlgorithm, InstanceContextAware {


    private Properties props;
    private volatile InstanceContext instanceContext;

    @Override
    public void init(final Properties props) {
        this.props = props;
    }


    @Override
    public Comparable<?> generateKey() {
        // 这里写 对接美团Leafid生成器  的逻辑，返回 生成的id


        return null;
    }

    @Override
    public String getType() {
        return "Leafid";
    }

    @Override
    public boolean isDefault() {
        return false;
    }


    @Override
    public void setInstanceContext(final InstanceContext instanceContext) {
        this.instanceContext = instanceContext;
        if (null != instanceContext) {
            instanceContext.generateWorkerId(props);
        }
    }
}
